﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using TravelWallet.Types;
using Microsoft.Advertising.Mobile.UI;
using Microsoft.Phone.Tasks;
using WindowsPhoneRecipes;
using System.IO.IsolatedStorage;

namespace TravelWallet
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            /*
            this.ad_AirlineAd.AdUnitId = "TextAd"; 
            this.ad_AirlineAd.ApplicationId = "test_client";*/

            /* OLD Code for Ad Unit
            this.ad_AirlineAd.AdUnitId = Constants.AIRLINE_ADUNIT;
            this.ad_AirlineAd.ApplicationId = Constants.APP_ID;

            this.ad_HotelAd.AdUnitId = Constants.HOTEL_ADUNIT;
            this.ad_HotelAd.ApplicationId = Constants.APP_ID;

            this.ad_CarAd.AdUnitId = Constants.CAR_ADUNIT;
            this.ad_CarAd.ApplicationId = Constants.APP_ID;
            
            //AdControl.TestMode = false;
             */

            if (!App.isTrialMode)
            {
                /*this.ad_AirlineAd.IsEnabled = false;
                this.ad_CarAd.IsEnabled = false;
                this.ad_HotelAd.IsEnabled = false;*/
                this.AdRotatorControl.IsEnabled = false;
                this.ad_upgradetofull.IsEnabled = false;

                /*this.ad_AirlineAd.Visibility = System.Windows.Visibility.Collapsed;
                this.ad_CarAd.Visibility = System.Windows.Visibility.Collapsed;
                this.ad_HotelAd.Visibility = System.Windows.Visibility.Collapsed;*/
                this.AdRotatorControl.Visibility = System.Windows.Visibility.Collapsed;
                this.ad_upgradetofull.Visibility = System.Windows.Visibility.Collapsed;

                this.p_Pivot.Height = 690;
            }
            else
            {
                /*this.ad_AirlineAd.IsEnabled = true;
                this.ad_CarAd.IsEnabled = true;
                this.ad_HotelAd.IsEnabled = true;*/
                this.AdRotatorControl.IsEnabled = true;
                this.ad_upgradetofull.IsEnabled = true;

                /*this.ad_AirlineAd.Visibility = System.Windows.Visibility.Visible;
                this.ad_CarAd.Visibility = System.Windows.Visibility.Visible;
                this.ad_HotelAd.Visibility = System.Windows.Visibility.Visible;*/
                this.AdRotatorControl.Visibility = System.Windows.Visibility.Visible;
                this.ad_upgradetofull.Visibility = System.Windows.Visibility.Visible;

                this.p_Pivot.Height = 590;
            }

            // Check to see if App Review request should show
            /*string counter;
            if (IsolatedStorageSettings.ApplicationSettings.TryGetValue<string>("startupCounter", out counter))
            {
                int counter2 = Convert.ToInt32(counter);
                if (counter2 == 10)
                {
                    if (MessageBox.Show("Please rate it in the marketplace.", "Love Travel Wallet?", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    {
                        MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
                        marketplaceReviewTask.Show();
                    }
                }
            }*/
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.lb_Airlines.DataContext = App.ViewModel.AirlineItems.OrderBy(f => f.Program.Name);
            this.lb_Hotels.DataContext = App.ViewModel.HotelItems.OrderBy(f => f.Program.ProgramName);
            this.lb_RentalCars.DataContext = App.ViewModel.RentalItems.OrderBy(f => f.Program.Name);

            this.lb_Airlines.ItemsSource = App.ViewModel.AirlineItems.OrderBy(f => f.Program.Name);
            this.lb_Hotels.ItemsSource = App.ViewModel.HotelItems.OrderBy(f => f.Program.ProgramName);
            this.lb_RentalCars.ItemsSource = App.ViewModel.RentalItems.OrderBy(f => f.Program.Name);

            // Check to see if App Review request should show
            string counter;
            string reviewReminderCount;

            if (IsolatedStorageSettings.ApplicationSettings.TryGetValue<string>("startupCounter", out counter))
            {
                if (IsolatedStorageSettings.ApplicationSettings.TryGetValue<string>("reviewReminderCount", out reviewReminderCount))
                {
                    int counterInt = Convert.ToInt32(counter);
                    int reviewReminderCountInt = Convert.ToInt32(reviewReminderCount);
                    if (counterInt == reviewReminderCountInt)
                    {
                        if (MessageBox.Show("Please rate it in the marketplace.", "Like Travel Wallet?", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                        {
                            counterInt++;
                            IsolatedStorageSettings.ApplicationSettings["startupCounter"] = Convert.ToString(counterInt);
                            IsolatedStorageSettings.ApplicationSettings.Save();
                            
                            MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
                            marketplaceReviewTask.Show();
                        }
                        else
                        {
                            reviewReminderCountInt = reviewReminderCountInt * 2;
                            IsolatedStorageSettings.ApplicationSettings["reviewReminderCount"] = Convert.ToString(reviewReminderCountInt);
                            IsolatedStorageSettings.ApplicationSettings.Save();
                        }
                    }
                }
            }
        }

        
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string category;

            if (NonLinearNavigationService.Instance.IsRecursiveBackNavigation == true)
            {
                return;
            }

            if (NavigationContext.QueryString.TryGetValue("category", out category))
            {
                ProgramCategory programCategory = (ProgramCategory)Enum.Parse(typeof(ProgramCategory), category, true);
                if (programCategory == ProgramCategory.Airlines)
                {
                    p_Pivot.SelectedIndex = 0;
                }
                else if (programCategory == ProgramCategory.Hotels)
                {
                    p_Pivot.SelectedIndex = 1;
                }
                else
                {
                    p_Pivot.SelectedIndex = 2;
                }
            }
        }

        //protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        //{
        //    //base.OnBackKeyPress(e);
        //    App.ViewModel.Close();
        //}


        private void b_Add_Click(object sender, EventArgs e)
        {
            string category = string.Empty;
            if (this.p_Pivot.SelectedIndex == 0)
            {
                category = "Airlines";
            }
            else if (this.p_Pivot.SelectedIndex == 1)
            {
                category = "Hotels";
            }
            else
            {
                category = "CarRentals";
            }

            NavigationService.Navigate(new Uri("/AddEditProgram.xaml?mode=add" + "&category=" + category, UriKind.Relative));
        }
        
        private void lb_Airlines_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            WalletItem selectedItem = lb_Airlines.SelectedItem as WalletItem;
            NavigateTo(selectedItem);
        }

        private void lb_Hotels_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
             WalletItem selectedItem = lb_Hotels.SelectedItem as WalletItem;
             NavigateTo(selectedItem);
        }

        private void lb_RentalCars_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
             WalletItem selectedItem = lb_RentalCars.SelectedItem as WalletItem;
             NavigateTo(selectedItem);
        }

        private void NavigateTo(WalletItem item) {
            if (item != null)
            {
                NavigationService.Navigate(new Uri("/ProgramDetails.xaml?itemId=" + item.ID, UriKind.Relative));
            }
        }

        private void airlines_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            WalletItem selectedItem = lb_Airlines.SelectedItem as WalletItem;
            if (selectedItem != null)
                NavigateTo(selectedItem);
        }

        private void hotels_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            WalletItem selectedItem = lb_Hotels.SelectedItem as WalletItem;
            if (selectedItem != null)
                NavigateTo(selectedItem);

        }

        private void cars_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            WalletItem selectedItem = lb_RentalCars.SelectedItem as WalletItem;
            if (selectedItem != null) 
                NavigateTo(selectedItem);
        }

        private void ApplicationBarMenuItem_Click_About(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/About.xaml", UriKind.Relative));
        }

        private void ApplicationBarMenuItem_Click_Rate(object sender, EventArgs e)
        {
            MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
            marketplaceReviewTask.Show();
        }

        private void ApplicationBarMenuItem_Click_Share(object sender, EventArgs e)
        {
            SmsComposeTask smsComposeTask = new SmsComposeTask();
            smsComposeTask.Body = "I am using this new app to keep all of my travel loyalty program numbers in one place. You should try it. zune://navigate/?appID=e8045962-583d-e011-854c-00237de2db9e";
            smsComposeTask.Show();
        }

        
    }
}